#include <fstream>
#include <iostream>
#include <string>

using namespace std;

void copy_file1(const string& src, const string& dest) {
	ifstream ifs(src, ios::in);
	ofstream ofs(dest, std::ofstream::out | std::ofstream::trunc);
	string buf;
	while(!ifs.eof()) {
		ifs >> buf;
		ofs << buf;
	}
	ifs.close();
	ofs.close();
	//当一个fstream对象被销毁时，会自动调用close去关闭它绑定的文件
}

void copy_file2(const string& src, const string& dest) {
	ifstream ifs(src, ios::in);
	ofstream ofs(dest, std::ofstream::out | std::ofstream::trunc);
	string buf;
	while(getline(ifs, buf))
	{
		ofs << buf+"\n";
	}
	ifs.close();
	ofs.close();
}

int main() {
	string srcname = "../test/bin/a.txt";
	string destname = "/home/lei/project/test/b.txt";
	//copy_file1(srcname, destname);
	copy_file2(srcname, destname);
	return 0;
}